﻿using Sunny.UI;
using SuperMap.Data;
using SuperMap.Realspace;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Ycjw.PipeLine.Map.Basic.SuperMap
{
    /// <summary>
    /// 数据图层列表：用于显示场景中的图层，这些图层对应的类型是Layer3DType.Dataset
    /// </summary>
    public class LayerWithDataListPanel: Sunny.UI.UIFlowLayoutPanel
    {
        public LayerWithDataListPanel() : base()
        {
            InitializeComponent();
            this.RectSides = ToolStripStatusLabelBorderSides.None;
            this.RadiusSides = UICornerRadiusSides.None;
            this.ParentChanged += LayerWithDataListPanel_ParentChanged;
        }
        /// <summary>
        /// 设置当前控件的外观Padding
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LayerWithDataListPanel_ParentChanged(object sender, EventArgs e)
        {
            if (this.Parent == null) return;
            if(this.Parent is UIGroupBox)
            {
                var ugb = this.Parent as UIGroupBox;
                ugb.Padding = new Padding(6, ugb.Padding.Top, 6, 4);
            }
        }

        bool m_MultiSelect = false; 
        Scene m_Scene;
        /// <summary>
        /// 场景
        /// </summary>
        public Scene Scene { get => m_Scene; }
        /// <summary>
        /// 是否支持多选，默认false
        /// </summary>
        public bool MultiSelect { get => m_MultiSelect; }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="scene"></param>
        public void Init(Scene scene)
        {
            m_Scene = scene;
        }
        /// <summary>
        /// 加载矢量图层列表
        /// </summary>
        /// <param name="multiSelect"></param>
        public virtual void LoadToVector(bool multiSelect = false)
        {
            var types = new List<DatasetType>() 
            {
                DatasetType.Point,
                DatasetType.Point3D,
                DatasetType.Line,
                DatasetType.Line3D ,
                DatasetType.Region,
                DatasetType.Region3D,
                DatasetType.Network,
                DatasetType.Network3D
            };
            LoadTo(types, multiSelect);
        }
        /// <summary>
        /// 加载矢量图层清单
        /// </summary>
        /// <param name="datasetTypes"></param>
        /// <param name="multiSelect"></param>
        public virtual void LoadTo(List<DatasetType> datasetTypes, bool multiSelect = false)
        {
            m_MultiSelect = multiSelect;
            foreach(Layer3D lyr in m_Scene.Layers)
            {
                if(lyr.Type == Layer3DType.Dataset)
                {                    
                    var name = lyr.Name;
                    var text = lyr.Caption;
                    if (!(lyr is Layer3DDataset)) continue;
                    var dataset = (lyr as Layer3DDataset).Dataset;

                    if (!datasetTypes.Contains(dataset.Type)) continue;

                    AddBtnInter(name, text, lyr);
                }
            }
        }
        /// <summary>
        /// 根据图层Name和Caption创建按钮，并将Layer3D绑定到按钮
        /// </summary>
        /// <param name="name"></param>
        /// <param name="text"></param>
        /// <param name="lyr">要绑定的Layer3D图层</param>
        private void AddBtnInter(string name,string text, Layer3D lyr)
        {
            if (m_MultiSelect)
            {
                Sunny.UI.UICheckBox cBtn = new UICheckBox()
                {
                    Name = name,
                    Text = text,
                    Tag = lyr,
                    Location = new System.Drawing.Point(20, 40 + this.Controls.Count * 28)
                };
                this.AddControl(cBtn);
                cBtn.ValueChanged += CBtn_ValueChanged;
            }
            else
            {
                Sunny.UI.UIRadioButton rBtn = new UIRadioButton()
                {
                    Name = name,
                    Text = text,
                    Tag = lyr,
                    Location = new System.Drawing.Point(20, 40 + this.Controls.Count * 28)
                };
                this.AddControl(rBtn);
                rBtn.ValueChanged += RBtn_ValueChanged;
            }
        }
        /// <summary>
        /// 按钮选中时获取值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void CBtn_ValueChanged(object sender, bool value)
        {
            var cBtn = sender as UICheckBox;
            if (cBtn.Checked)
            {
                m_SelectLayer3DDatasets.Add(cBtn.Tag as Layer3DDataset);
            }
            else
            {
                m_SelectLayer3DDatasets.Remove(cBtn.Tag as Layer3DDataset);
            }
        }
        /// <summary>
        /// 单选按钮选中时候设置值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void RBtn_ValueChanged(object sender, bool value)
        {
            var rBtn = sender as UIRadioButton;
            if (!rBtn.Checked) return;
            m_SelectLayer3DDataset = rBtn.Tag as Layer3DDataset;
        }

        Layer3DDataset m_SelectLayer3DDataset;
        /// <summary>
        /// 单选按钮时获取选中图层对应的数据集
        /// </summary>
        public Layer3DDataset SelectLayer3DDataset
        {
            get
            {
                return m_SelectLayer3DDataset;
            }
        }

        List<Layer3DDataset> m_SelectLayer3DDatasets = new List<Layer3DDataset>();
        /// <summary>
        /// 多选按钮时获取选中图层对应的数据集
        /// </summary>
        public List<Layer3DDataset> SelectLayer3DDatasets
        {
            get
            {
                return m_SelectLayer3DDatasets;
            }
        }
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // LayerWithDataListPanel
            // 
            this.Name = "LayerWithDataListPanel";
            this.Size = new System.Drawing.Size(271, 387);
            this.ResumeLayout(false);

        }
    }
}
